package com.oracle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oracle.compents.ApiResponse;
import com.oracle.pojo.Equipment;
import com.oracle.pojo.Record;
import com.oracle.service.RecordService;

import com.oracle.vo.DeleteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @GetMapping("/findOne")
    public ApiResponse findOne(Integer id) {
        Record oneRecord = recordService.findOneRecord(id);
        if (ObjectUtils.isEmpty(oneRecord)) {
            return ApiResponse.failed(null);
        } else {
            return ApiResponse.success(oneRecord);
        }
    }

    @GetMapping("/findAll")
    public ApiResponse findAll(Integer pageNum, Integer pageSize) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("record_Time");
        IPage<Record> recordPage = recordService.findRecordPage(pageNum, pageSize, queryWrapper);
        if (recordPage != null && !recordPage.getRecords().isEmpty()) {
            // 查询结果不为空，进行相应处理

            ApiResponse<IPage<Record>> success = ApiResponse.successIPage(recordPage);

            success.setMsg("查询成功");

            return success;
        } else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("首页模块，查询不到记录");
            return failed;
        }

    }

    @GetMapping("/findAllRecycle")
    public ApiResponse findAllRecycle(Integer pageNum, Integer pageSize) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 1);
        queryWrapper.orderByDesc("record_Time");
        IPage<Record> recordPage = recordService.findRecordPage(pageNum, pageSize, queryWrapper);
        if (recordPage != null && !recordPage.getRecords().isEmpty()) {
            // 查询结果不为空，进行相应处理

            ApiResponse<IPage<Record>> success = ApiResponse.successIPage(recordPage);

            success.setMsg("查询成功");

            return success;
        } else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("首页模块，查询不到记录");
            return failed;
        }

    }

    @PostMapping("/findCondition")
    public ApiResponse findCondition(@RequestBody Record record, Integer pageNum, Integer pageSize) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        Date recordTime = record.getRecordTime();
        String stuName1 = (record.getOutName() != null) ? record.getOutName() : null;
        String stuName2 = (record.getInName() != null) ? record.getInName() : null;
        String equipmentName = (record.getEqName() != null) ? record.getEqName() : null;
        if (null != recordTime) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedRecordTime = formatter.format(recordTime);
            queryWrapper.like("record_Time", formattedRecordTime);
        }

        if (null != stuName1) {
            queryWrapper.like("s1.stu_Name", stuName1);
        }

        if (null != stuName2) {
            queryWrapper.like("s2.stu_Name", stuName2);
        }

        if (null != equipmentName) {
            queryWrapper.like("eq.equipment_Name", equipmentName);
        }
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("record_Time");
        IPage<Record> recordPage = recordService.findRecordPage(pageNum, pageSize, queryWrapper);
        if (recordPage != null && !recordPage.getRecords().isEmpty()) {
            // 查询结果不为空，进行相应处理

            ApiResponse<IPage<Record>> success = ApiResponse.successIPage(recordPage);

            success.setMsg("查询成功");

            return success;
        } else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("记录模块，查询不到记录");
            return failed;
        }

    }

    @PostMapping("/findPersonRecord")
    public ApiResponse findPersonRecord(@RequestBody Record record, Integer pageNum, Integer pageSize) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        Date recordTime = record.getRecordTime();
        String stuName1 = (record.getOutName() != null) ? record.getOutName() : null;
        String equipmentName = (record.getEqName() != null) ? record.getEqName() : null;
        if (null != recordTime) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedRecordTime = formatter.format(recordTime);
            queryWrapper.like("record_Time", formattedRecordTime);
        }

        if (null != stuName1) {
            queryWrapper.like("s1.stu_Name", stuName1);
        }

        if (null != equipmentName) {
            queryWrapper.like("eq.equipment_Name", equipmentName);
        }
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("record_Time");
        IPage<Record> recordPage = recordService.findRecordPage(pageNum, pageSize, queryWrapper);
        if (recordPage != null && !recordPage.getRecords().isEmpty()) {
            // 查询结果不为空，进行相应处理

            ApiResponse<IPage<Record>> success = ApiResponse.successIPage(recordPage);

            success.setMsg("查询成功");

            return success;
        } else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("记录模块，查询不到记录");
            return failed;
        }

    }


    @PostMapping("/findConditionRecycle")
    public ApiResponse findConditionRecycle(@RequestBody Record record, Integer pageNum, Integer pageSize) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        Date recordTime = record.getRecordTime();
        String stuName1 = (record.getOutName() != null) ? record.getOutName() : null;
        String stuName2 = (record.getInName() != null) ? record.getInName() : null;
        String equipmentName = (record.getEqName() != null) ? record.getEqName() : null;
        if (null != recordTime) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedRecordTime = formatter.format(recordTime);
            queryWrapper.like("record_Time", formattedRecordTime);
        }

        if (null != stuName1) {
            queryWrapper.like("s1.stu_Name", stuName1);
        }

        if (null != stuName2) {
            queryWrapper.like("s2.stu_Name", stuName2);
        }

        if (null != equipmentName) {
            queryWrapper.like("eq.equipment_Name", equipmentName);
        }
        queryWrapper.eq("is_delete", 1);
        queryWrapper.orderByDesc("record_Time");
        IPage<Record> recordPage = recordService.findRecordPage(pageNum, pageSize, queryWrapper);
        if (recordPage != null && !recordPage.getRecords().isEmpty()) {
            // 查询结果不为空，进行相应处理

            ApiResponse<IPage<Record>> success = ApiResponse.successIPage(recordPage);

            success.setMsg("查询成功");

            return success;
        } else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("记录模块，查询不到记录");
            return failed;
        }

    }

    @PostMapping("/addRecord")
    public ApiResponse addRecord(@RequestBody Record record) {
        record.setIsDelete(0);
        int i = recordService.insertRecord(record);
        if (i > 0) {
            ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(null);
            success.setMsg("添加成功");
            return success;
        } else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("记录模块，添加失败");
            return failed;
        }
    }

    @PostMapping("/updataRecord")
    public ApiResponse updataRecord(@RequestBody Record record){
        int i = recordService.updataRecord(record);
        if (i>0){
            ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(null);
            success.setMsg("修改成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("记录模块，修改失败");
            return failed;
        }
    }

    @PostMapping("/deleteRecord")
    public ApiResponse deleteRecord(@RequestBody DeleteRequest deleteRequest){
        try {
            List<Integer> ids = deleteRequest.getIds();
            int i = recordService.deleteRecord(ids);
            if (i > 0){
                return ApiResponse.success(null);
            }else {
                return ApiResponse.failed(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failed(null);
        }

    }
}
